using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class TattooSubMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.tatos_Menu, new Point(0, -107));

	private static List<dynamic> collectionList = new List<object>(new string[1] { string.Empty });

	private static List<dynamic> overlayView = new List<object>(new string[1] { string.Empty });

	private static List<dynamic> overlayList = new List<object>(new string[1] { string.Empty });

	private static UIMenuListItem collectionItem;

	private static UIMenuListItem overlayItem;

	private static int zid;

	private static int gid;

	public TattooSubMenu()
	{
		Main.pool.Add(menu);
		menu.OnListChange += OnListChange;
		menu.OnItemSelect += OnItemSelect;
		menu.OnMenuClose += OnMenuClose;
	}

	public static void InitTattooSubMenu(int _zid, int _gid)
	{
		zid = _zid;
		gid = _gid;
		menu.Clear();
		collectionList = new List<object>();
		overlayView = new List<object>();
		overlayList = new List<object>();
		List<Decoration.Collection> list;
		Skin.Tattoos tattoos;
		if (DressingScript.Dressing == Main.Member.P2)
		{
			if (Main.P2.skin.TattooData == null)
			{
				Main.P2.skin.GetDecorationData(Decoration.Type.TYPE_TATTOO);
			}
			list = Main.P2.skin.TattooData[zid];
			tattoos = Main.P2.skin.Tattoo[zid][gid];
		}
		else
		{
			if (Main.P1.skin.TattooData == null)
			{
				Main.P1.skin.GetDecorationData(Decoration.Type.TYPE_TATTOO);
				Main.Clone.skin.GetDecorationData(Decoration.Type.TYPE_TATTOO);
			}
			list = Main.P1.skin.TattooData[zid];
			tattoos = Main.P1.skin.Tattoo[zid][gid];
		}
		foreach (Decoration.Collection item in list)
		{
			collectionList.Add(item.nameHash);
		}
		int num = collectionList.FindIndex(name => name == tattoos.collectionName);
		if (num == -1)
		{
			num = 0;
		}
		foreach (Decoration.Collection.Overlay item2 in list[num].overlay)
		{
			if (item2.award != string.Empty)
			{
				overlayView.Add(item2.award);
				overlayList.Add(item2.nameHash);
			}
			else
			{
				overlayView.Add(item2.txtHash);
				overlayList.Add(item2.nameHash);
			}
		}
		int num2 = overlayList.FindIndex(name => name == tattoos.overlayName);
		if (num2 == -1)
		{
			num2 = 0;
		}
		collectionItem = new UIMenuListItem(TextSetting.tbs_ListItem_Collection, collectionList, num);
		overlayItem = new UIMenuListItem(TextSetting.tbs_ListItem_Overlay, overlayView, num2);
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(collectionItem);
		menu.AddItem(overlayItem);
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Randomize));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Clear));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
		menu.RefreshIndex();
	}

	public static void UpdateTattooValue(bool isMenuExit = false)
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			if (!isMenuExit || Main.P2.skin.Tattoo[zid][gid].action)
			{
				Main.P2.skin.Tattoo[zid][gid].collectionName = collectionList[collectionItem.Index];
				Main.P2.skin.Tattoo[zid][gid].overlayName = overlayList[overlayItem.Index];
			}
		}
		else if (!isMenuExit || Main.P1.skin.Tattoo[zid][gid].action)
		{
			Main.P1.skin.Tattoo[zid][gid].collectionName = collectionList[collectionItem.Index];
			Main.P1.skin.Tattoo[zid][gid].overlayName = overlayList[overlayItem.Index];
			Main.Clone.skin.Tattoo[zid][gid].collectionName = collectionList[collectionItem.Index];
			Main.Clone.skin.Tattoo[zid][gid].overlayName = overlayList[overlayItem.Index];
		}
	}

	private void OnListChange(UIMenu sender, UIMenuListItem list, int index)
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			if (list.Text == sender.MenuItems[0].Text)
			{
				Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
				SkinHelper.SetPedDecoration(Main.P2.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
				UpdateTattooValue();
				InitTattooSubMenu(zid, gid);
			}
			Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
			SkinHelper.SetPedDecoration(Main.P2.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
			Main.P2.skin.Tattoo[zid][gid].action = true;
			return;
		}
		if (list.Text == sender.MenuItems[0].Text)
		{
			Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
			Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
			SkinHelper.SetPedDecoration(Main.P1.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
			SkinHelper.SetPedDecoration(Main.Clone.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
			UpdateTattooValue();
			InitTattooSubMenu(zid, gid);
		}
		Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
		Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
		SkinHelper.SetPedDecoration(Main.P1.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
		SkinHelper.SetPedDecoration(Main.Clone.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
		Main.P1.skin.Tattoo[zid][gid].action = true;
		Main.Clone.skin.Tattoo[zid][gid].action = true;
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		switch (index)
		{
		case 2:
		{
			Random random = new Random(Guid.NewGuid().GetHashCode());
			collectionItem.Index = random.Next(0, collectionList.Count);
			UpdateTattooValue();
			InitTattooSubMenu(zid, gid);
			overlayItem.Index = random.Next(0, overlayList.Count);
			if (DressingScript.Dressing == Main.Member.P2)
			{
				Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
				SkinHelper.SetPedDecoration(Main.P2.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
				Main.P2.skin.Tattoo[zid][gid].action = true;
			}
			else
			{
				Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
				Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
				SkinHelper.SetPedDecoration(Main.P1.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
				SkinHelper.SetPedDecoration(Main.Clone.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
				Main.P1.skin.Tattoo[zid][gid].action = true;
				Main.Clone.skin.Tattoo[zid][gid].action = true;
			}
			sender.CurrentSelection = 2;
			break;
		}
		case 3:
		{
			UIMenuListItem uIMenuListItem = collectionItem;
			int index2 = (overlayItem.Index = 0);
			uIMenuListItem.Index = index2;
			if (DressingScript.Dressing == Main.Member.P2)
			{
				Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
				Main.P2.skin.Tattoo[zid][gid].collectionName = (Main.P2.skin.Tattoo[zid][gid].overlayName = string.Empty);
				Main.P2.skin.Tattoo[zid][gid].action = false;
			}
			else
			{
				Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
				Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_TATTOO, gid, (Decoration.Zone)zid);
				Main.P1.skin.Tattoo[zid][gid].collectionName = (Main.P1.skin.Tattoo[zid][gid].overlayName = (Main.Clone.skin.Tattoo[zid][gid].collectionName = (Main.Clone.skin.Tattoo[zid][gid].overlayName = string.Empty)));
				Main.P1.skin.Tattoo[zid][gid].action = false;
				Main.Clone.skin.Tattoo[zid][gid].action = false;
			}
			InitTattooSubMenu(zid, gid);
			break;
		}
		case 4:
			DressingScript.Distance();
			break;
		case 5:
			DressingScript.TurnLeft();
			break;
		case 6:
			DressingScript.TurnRight();
			break;
		case 7:
			DressingScript.AngleUp();
			break;
		case 8:
			DressingScript.AngleDown();
			break;
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnMenuClose(UIMenu sender)
	{
		UpdateTattooValue(isMenuExit: true);
		menu.Visible = false;
		menu.RefreshIndex();
		TattooMenu.InitTattooMenu();
		TattooMenu.menu.CurrentSelection = zid + 1;
	}
}
